package linked;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 邹陆川
 * @Date: 2022/04/19/7:53 下午
 * @Description:反转链表
 */
public class ReverseList {
  public static void main(String[] args) {

  }
  private ListNode reverseList(ListNode head) {
    Stack<ListNode> stack = new Stack<>();
    //将链表存入栈
    while (head != null){
      stack.push(head);
      //下一个节点
      head = head.next;
    }
    //为空则返回
    if(stack.empty()){return null;}
    //弹栈
    ListNode temp = stack.pop();
    ListNode newNode = temp;
    //不为空则继续添加
    while(!stack.empty()){
      ListNode temp2 =  stack.pop();
      temp.next = temp2;
      temp = temp.next;
    }
    //将最后一个值设空
    temp.next = null;
    return newNode;
  }
  private class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
}
